<?php  

class Mail_controller extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
	}
	function index()
	{
		$this->load->model('user_model');
		$profile['verification_code'] = $this->input->get('vericode');
		
		$res = $this->user_model->get_user($profile);
		$d['is_valid_code'] = ($res->num_rows() > 0);
		$row = $this->user_model->ChangeQueryResultToArray( $res );
		if($d['is_valid_code'])
		{
			$d['is_verified'] = $row[0]->is_verified;
			if(!$d['is_verified'])
			{
				$data['userid'] = $row[0]->userid;
				$data['is_verified'] = true;
				$this->user_model->update($data);
			}			
		}
		$this->load->view('mail_view',$d);
	}
	
	function get_random_verification_code()
	{
		$index[0] = '1';	$index[1] = '2';	$index[2] = '3';	$index[3] = '4';	$index[4] = '5';
		$index[5] = '6';	$index[6] = '7';	$index[7] = '8';	$index[8] = '9';	$index[9] = 'a';
		$index[10] = 'b';	$index[11] = 'c';	$index[12] = 'd';	$index[13] = 'e';	$index[14] = 'f';
		$index[15] = 'g';	$index[16] = 'h';	$index[17] = 'i';	$index[18] = 'j';	$index[19] = 'k';
		$index[20] = 'l';	$index[21] = 'm';	$index[22] = 'n';	$index[23] = 'o';	$index[24] = 'p';
		$index[25] = 'q';	$index[26] = 'r';	$index[27] = 's';	$index[28] = 't';	$index[29] = 'u';
		$index[30] = 'v';	$index[31] = 'w';	$index[32] = 'x';	$index[33] = 'y';	$index[34] = 'z';
		$index[35] = 'A';	$index[36] = 'B';	$index[37] = 'C';	$index[38] = 'D';	$index[39] = 'E';
		$index[40] = 'F';	$index[41] = 'G';	$index[42] = 'H';	$index[43] = 'I';	$index[44] = 'J';
		$index[45] = 'K';	$index[46] = 'L';	$index[47] = 'M';	$index[48] = 'N';	$index[49] = 'O';
		$index[50] = 'P';	$index[51] = 'Q';	$index[52] = 'R';	$index[53] = 'S';	$index[54] = 'T';
		$index[55] = 'U';	$index[56] = 'V';	$index[57] = 'W';	$index[58] = 'X';	$index[59] = 'Y';
		$index[60] = 'Z';
		$code='';
		for( $i = 0; $i < 64; $i++)
			$code=$code.$index[( mt_rand() % 61 )];
		return $code;
	}
	
	function send_email() 
	{
		$this->load->model('user_model');
		$code = $this->get_random_verification_code();
		$config['protocol'] = 'smtp';
		$config['smtp_host'] = 'ssl://smtp.googlemail.com';
		$config['smtp_port'] = 465;
		$config['smtp_user'] = 'hellogue@gmail.com';
		$config['smtp_pass'] = 'gondoprastowo';
		
			$this->load->library('email',$config);
			$this->email->set_newline("\r\n");
			$this->email->from('hellogue@gmail.com', 'Hellogue Team');
			$this->email->to( $this->session->userdata('email') );		
			$this->email->subject('Verification Link for Hellogue Registration');		
			$this->email->message(
			site_url('mail_controller?=').$code);
			$this->email->send();
			
			$profile['userid'] = $this->session->userdata('userid');
			$profile['verification_code'] = $code;
			$this->user_model->update($profile);
			
			redirect('/feed_controller/index');	
	}
}

	

